home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / tk2.3 / dist / tkColor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-14  |  12.2 KB  |  418 lines

  1. /* 
  2.  * tkColor.c --
  3.  *
  4.  *    This file maintains a database of color values for the Tk
  5.  *    toolkit, in order to avoid round-trips to the server to
  6.  *    map color names to pixel values.
  7.  *
  8.  * Copyright 1990 Regents of the University of California
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software and its documentation for any purpose and without
  11.  * fee is hereby granted, provided that the above copyright
  12.  * notice appear in all copies.  The University of California
  13.  * makes no representations about the suitability of this
  14.  * software for any purpose.  It is provided "as is" without
  15.  * express or implied warranty.
  16.  */
  17.  
  18. #ifndef lint
  19. static char rcsid[] = "$Header: /user6/ouster/wish/RCS/tkColor.c,v 1.15 92/07/14 08:44:49 ouster Exp $ SPRITE (Berkeley)";
  20. #endif /* not lint */
  21.  
  22. #include "tkConfig.h"
  23. #include "tk.h"
  24.  
  25. /*
  26.  * A two-level data structure is used to manage the color database.
  27.  * The top level consists of one entry for each color name that is
  28.  * currently active, and the bottom level contains one entry for each
  29.  * pixel value that is still in use.  The distinction between
  30.  * levels is necessary because the same pixel may have several
  31.  * different names.  There are two hash tables, one used to index into
  32.  * each of the data structures.  The name hash table is used when
  33.  * allocating colors, and the pixel hash table is used when freeing
  34.  * colors.
  35.  */
  36.  
  37. /*
  38.  * One of the following data structures is used to keep track of
  39.  * each color that this module has allocated from the X display
  40.  * server.  These entries are indexed by two hash tables defined
  41.  * below:  nameTable and valueTable.
  42.  */
  43.  
  44. #define COLOR_MAGIC 0xc6140277
  45.  
  46. typedef struct TkColor {
  47.     XColor color;        /* Information about this color. */
  48.     int magic;            /* Used for quick integrity check on this
  49.                  * structure.   Must always have the
  50.                  * value COLOR_MAGIC. */
  51.     Screen *screen;        /* Screen where this color is valid.  Used
  52.                  * to delete it. */
  53.     Colormap colormap;        /* Colormap from which this entry was
  54.                  * allocated. */
  55.     int refCount;        /* Number of uses of this structure. */
  56.     Tcl_HashTable *tablePtr;    /* Hash table that indexes this structure
  57.                  * (needed when deleting structure). */
  58.     Tcl_HashEntry *hashPtr;    /* Pointer to hash table entry for this
  59.                  * structure. (for use in deleting entry). */
  60. } TkColor;
  61.  
  62. /*
  63.  * Hash table for name -> TkColor mapping, and key structure used to
  64.  * index into that table:
  65.  */
  66.  
  67. static Tcl_HashTable nameTable;
  68. typedef struct {
  69.     Tk_Uid name;        /* Name of desired color. */
  70.     Colormap colormap;        /* Colormap from which color will be
  71.                  * allocated. */
  72.     Display *display;        /* Display for colormap. */
  73. } NameKey;
  74.  
  75. /*
  76.  * Hash table for value -> TkColor mapping, and key structure used to
  77.  * index into that table:
  78.  */
  79.  
  80. static Tcl_HashTable valueTable;
  81. typedef struct {
  82.     int red, green, blue;    /* Values for desired color. */
  83.     Colormap colormap;        /* Colormap from which color will be
  84.                  * allocated. */
  85.     Display *display;        /* Display for colormap. */
  86. } ValueKey;
  87.  
  88. static int initialized = 0;    /* 0 means static structures haven't been
  89.                  * initialized yet. */
  90.  
  91. /*
  92.  * Forward declarations for procedures defined in this file:
  93.  */
  94.  
  95. static void        ColorInit _ANSI_ARGS_((void));
  96.  
  97. /*
  98.  *----------------------------------------------------------------------
  99.  *
  100.  * Tk_GetColor --
  101.  *
  102.  *    Given a string name for a color, map the name to a corresponding
  103.  *    XColor structure.
  104.  *
  105.  * Results:
  106.  *    The return value is a pointer to an XColor structure that
  107.  *    indicates the red, blue, and green intensities for the color
  108.  *    given by "name", and also specifies a pixel value to use to
  109.  *    draw in that color in window "tkwin".  If an error occurs,
  110.  *    then NULL is returned and an error message will be left in
  111.  *    interp->result.
  112.  *
  113.  * Side effects:
  114.  *    The color is added to an internal database with a reference count.
  115.  *    For each call to this procedure, there should eventually be a call
  116.  *    to Tk_FreeColor, so that the database is cleaned up when colors
  117.  *    aren't in use anymore.
  118.  *
  119.  *----------------------------------------------------------------------
  120.  */
  121.  
  122. XColor *
  123. Tk_GetColor(interp, tkwin, colormap, name)
  124.     Tcl_Interp *interp;        /* Place to leave error message if
  125.                  * color can't be found. */
  126.     Tk_Window tkwin;        /* Window in which color will be used. */
  127.     Colormap colormap;        /* Map from which to allocate color.  None
  128.                  * means use default. */
  129.     Tk_Uid name;        /* Name of color to allocated (in form
  130.                  * suitable for passing to XParseColor). */
  131. {
  132.     NameKey nameKey;
  133.     Tcl_HashEntry *nameHashPtr;
  134.     int new;
  135.     TkColor *tkColPtr;
  136.     XColor color;
  137.  
  138.     if (!initialized) {
  139.     ColorInit();
  140.     }
  141.  
  142.     /*
  143.      * First, check to see if there's already a mapping for this color
  144.      * name.
  145.      */
  146.  
  147.     nameKey.name = name;
  148.     if (colormap == None) {
  149.     colormap = XDefaultColormapOfScreen(Tk_Screen(tkwin));
  150.     }
  151.     nameKey.colormap = colormap;
  152.     nameKey.display = Tk_Display(tkwin);
  153.     nameHashPtr = Tcl_CreateHashEntry(&nameTable, (char *) &nameKey, &new);
  154.     if (!new) {
  155.     tkColPtr = (TkColor *) Tcl_GetHashValue(nameHashPtr);
  156.     tkColPtr->refCount++;
  157.     return &tkColPtr->color;
  158.     }
  159.  
  160.     /*
  161.      * The name isn't currently known.  Map from the name to a pixel
  162.      * value.  Be tricky here, and call XAllocNamedColor instead of
  163.      * XParseColor for non-# names:  this saves a server round-trip
  164.      * for those names.
  165.      */
  166.  
  167.     if (*name != '#') {
  168.     XColor screen;
  169.  
  170.     if (XAllocNamedColor(Tk_Display(tkwin), colormap, name,
  171.         &screen, &color) == 0) {
  172.         allocFailed:
  173.         Tcl_AppendResult(interp, "couldn't allocate a color for \"",
  174.             name, "\"", (char *) NULL);
  175.         Tcl_DeleteHashEntry(nameHashPtr);
  176.         return (XColor *) NULL;
  177.     }
  178.     } else {
  179.     if (XParseColor(Tk_Display(tkwin), colormap, name, &color) == 0) {
  180.         Tcl_AppendResult(interp, "invalid color name \"", name,
  181.             "\"", (char *) NULL);
  182.         Tcl_DeleteHashEntry(nameHashPtr);
  183.         return (XColor *) NULL;
  184.     }
  185.     if (XAllocColor(Tk_Display(tkwin), colormap, &color) == 0) {
  186.         goto allocFailed;
  187.     }
  188.     }
  189.  
  190.     /*
  191.      * Now create a new TkColor structure and add it to nameTable.
  192.      */
  193.  
  194.     tkColPtr = (TkColor *) ckalloc(sizeof(TkColor));
  195.     tkColPtr->color = color;
  196.     tkColPtr->magic = COLOR_MAGIC;
  197.     tkColPtr->screen = Tk_Screen(tkwin);
  198.     tkColPtr->colormap = colormap;
  199.     tkColPtr->refCount = 1;
  200.     tkColPtr->tablePtr = &nameTable;
  201.     tkColPtr->hashPtr = nameHashPtr;
  202.     Tcl_SetHashValue(nameHashPtr, tkColPtr);
  203.  
  204.     return &tkColPtr->color;
  205. }
  206.  
  207. /*
  208.  *----------------------------------------------------------------------
  209.  *
  210.  * Tk_GetColorByValue --
  211.  *
  212.  *    Given a desired set of red-green-blue intensities for a color,
  213.  *    locate a pixel value to use to draw that color in a given
  214.  *    window.
  215.  *
  216.  * Results:
  217.  *    The return value is a pointer to an XColor structure that
  218.  *    indicates the closest red, blue, and green intensities available
  219.  *    to those specified in colorPtr, and also specifies a pixel
  220.  *    value to use to draw in that color in window "tkwin".  If an
  221.  *    error occurs, then NULL is returned and an error message will
  222.  *    be left in interp->result.
  223.  *
  224.  * Side effects:
  225.  *    The color is added to an internal database with a reference count.
  226.  *    For each call to this procedure, there should eventually be a call
  227.  *    to Tk_FreeColor, so that the database is cleaned up when colors
  228.  *    aren't in use anymore.
  229.  *
  230.  *----------------------------------------------------------------------
  231.  */
  232.  
  233. XColor *
  234. Tk_GetColorByValue(interp, tkwin, colormap, colorPtr)
  235.     Tcl_Interp *interp;        /* Place to leave error message if
  236.                  * color can't be found. */
  237.     Tk_Window tkwin;        /* Window in which color will be used. */
  238.     Colormap colormap;        /* Map from which to allocate color.  None
  239.                  * means use default. */
  240.     XColor *colorPtr;        /* Red, green, and blue fields indicate
  241.                  * desired color. */
  242. {
  243.     ValueKey valueKey;
  244.     Tcl_HashEntry *valueHashPtr;
  245.     int new;
  246.     TkColor *tkColPtr;
  247.  
  248.     if (!initialized) {
  249.     ColorInit();
  250.     }
  251.  
  252.     /*
  253.      * First, check to see if there's already a mapping for this color
  254.      * name.
  255.      */
  256.  
  257.     valueKey.red = colorPtr->red;
  258.     valueKey.green = colorPtr->green;
  259.     valueKey.blue = colorPtr->blue;
  260.     if (colormap == None) {
  261.     colormap = XDefaultColormapOfScreen(Tk_Screen(tkwin));
  262.     }
  263.     valueKey.colormap = colormap;
  264.     valueKey.display = Tk_Display(tkwin);
  265.     valueHashPtr = Tcl_CreateHashEntry(&valueTable, (char *) &valueKey, &new);
  266.     if (!new) {
  267.     tkColPtr = (TkColor *) Tcl_GetHashValue(valueHashPtr);
  268.     tkColPtr->refCount++;
  269.     return &tkColPtr->color;
  270.     }
  271.  
  272.     /*
  273.      * The name isn't currently known.  Find a pixel value for this
  274.      * color and add a new structure to valueTable.
  275.      */
  276.  
  277.     tkColPtr = (TkColor *) ckalloc(sizeof(TkColor));
  278.     tkColPtr->color.red = valueKey.red;
  279.     tkColPtr->color.green = valueKey.green;
  280.     tkColPtr->color.blue = valueKey.blue;
  281.     if (XAllocColor(Tk_Display(tkwin), colormap, &tkColPtr->color) == 0) {
  282.     sprintf(interp->result, "couldn't allocate color");
  283.     Tcl_DeleteHashEntry(valueHashPtr);
  284.     ckfree((char *) tkColPtr);
  285.     return (XColor *) NULL;
  286.     }
  287.     tkColPtr->magic = COLOR_MAGIC;
  288.     tkColPtr->screen = Tk_Screen(tkwin);
  289.     tkColPtr->colormap = colormap;
  290.     tkColPtr->refCount = 1;
  291.     tkColPtr->tablePtr = &valueTable;
  292.     tkColPtr->hashPtr = valueHashPtr;
  293.     Tcl_SetHashValue(valueHashPtr, tkColPtr);
  294.  
  295.     return &tkColPtr->color;
  296. }
  297.  
  298. /*
  299.  *--------------------------------------------------------------
  300.  *
  301.  * Tk_NameOfColor --
  302.  *
  303.  *    Given a color, return a textual string identifying
  304.  *    the color.
  305.  *
  306.  * Results:
  307.  *    If colorPtr was created by Tk_GetColor, then the return
  308.  *    value is the "string" that was used to create it.
  309.  *    Otherwise the return value is a string that could have
  310.  *    been passed to Tk_GetColor to allocate that color.  The
  311.  *    storage for the returned string is only guaranteed to
  312.  *    persist up until the next call to this procedure.
  313.  *
  314.  * Side effects:
  315.  *    None.
  316.  *
  317.  *--------------------------------------------------------------
  318.  */
  319.  
  320. char *
  321. Tk_NameOfColor(colorPtr)
  322.     XColor *colorPtr;        /* Color whose name is desired. */
  323. {
  324.     register TkColor *tkColPtr = (TkColor *) colorPtr;
  325.     static char string[20];
  326.  
  327.     if ((tkColPtr->magic == COLOR_MAGIC)
  328.         && (tkColPtr->tablePtr == &nameTable)) {
  329.     return ((NameKey *) tkColPtr->hashPtr->key.words)->name;
  330.     }
  331.     sprintf(string, "#%4x%4x%4x", colorPtr->red, colorPtr->green,
  332.         colorPtr->blue);
  333.     return string;
  334. }
  335.  
  336. /*
  337.  *----------------------------------------------------------------------
  338.  *
  339.  * Tk_FreeColor --
  340.  *
  341.  *    This procedure is called to release a color allocated by
  342.  *    Tk_GetColor.
  343.  *
  344.  * Results:
  345.  *    None.
  346.  *
  347.  * Side effects:
  348.  *    The reference count associated with colorPtr is deleted, and
  349.  *    the color is released to X if there are no remaining uses
  350.  *    for it.
  351.  *
  352.  *----------------------------------------------------------------------
  353.  */
  354.  
  355. void
  356. Tk_FreeColor(colorPtr)
  357.     XColor *colorPtr;        /* Color to be released.  Must have been
  358.                  * allocated by Tk_GetColor or
  359.                  * Tk_GetColorByValue. */
  360. {
  361.     register TkColor *tkColPtr = (TkColor *) colorPtr;
  362.     Visual *visual;
  363.     Screen *screen = tkColPtr->screen;
  364.  
  365.     /*
  366.      * Do a quick sanity check to make sure this color was really
  367.      * allocated by Tk_GetColor.
  368.      */
  369.  
  370.     if (tkColPtr->magic != COLOR_MAGIC) {
  371.     panic("Tk_FreeColor called with bogus color");
  372.     }
  373.  
  374.     tkColPtr->refCount--;
  375.     if (tkColPtr->refCount == 0) {
  376.  
  377.     /*
  378.      * Careful!  Don't free black or white, since this will
  379.      * make some servers very unhappy.
  380.      */
  381.  
  382.     visual = DefaultVisualOfScreen(screen);
  383.     if ((visual->class != StaticGray) && (visual->class != StaticColor)
  384.         && (tkColPtr->color.pixel != BlackPixelOfScreen(screen))
  385.         && (tkColPtr->color.pixel != WhitePixelOfScreen(screen))) {
  386.         XFreeColors(DisplayOfScreen(screen), tkColPtr->colormap,
  387.             &tkColPtr->color.pixel, 1, 0L);
  388.     }
  389.     Tcl_DeleteHashEntry(tkColPtr->hashPtr);
  390.     tkColPtr->magic = 0;
  391.     ckfree((char *) tkColPtr);
  392.     }
  393. }
  394.  
  395. /*
  396.  *----------------------------------------------------------------------
  397.  *
  398.  * ColorInit --
  399.  *
  400.  *    Initialize the structure used for color management.
  401.  *
  402.  * Results:
  403.  *    None.
  404.  *
  405.  * Side effects:
  406.  *    Read the code.
  407.  *
  408.  *----------------------------------------------------------------------
  409.  */
  410.  
  411. static void
  412. ColorInit()
  413. {
  414.     initialized = 1;
  415.     Tcl_InitHashTable(&nameTable, sizeof(NameKey)/sizeof(int));
  416.     Tcl_InitHashTable(&valueTable, sizeof(ValueKey)/sizeof(int));
  417. }
  418.